package com.ah._06batch;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.util.ArrayList;
import java.util.List;

/**
 * 生产者
 */
public class Produce {
    public static void main(String[] args) throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        //创建生产者对象
        DefaultMQProducer dmp = new DefaultMQProducer("batch_produce_group");
        //设置命名空间地址
        dmp.setNamesrvAddr("192.168.19.132:9876");
        //启动生产者
        dmp.start();

        //创建集合存储消息
        List<Message> messageList = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            //创建消息对象
            Message message = new Message("batch_topic", "", ("批量消息"+i).getBytes());

            //将消息对象添加到集合
            messageList.add(message);
        }

        //发送消息
        dmp.send(messageList);

        //关闭生产者impletopic
        dmp.shutdown();
    }
}
